﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace Gameplay.PVE
{
    public class PveTaskManager
    {
        private static PveTaskManager m_instance;

        private class PveTask
        {
            public Action action;
            public int weight;
            public float registTime;
        }

        private Queue<PveTask> tasks = new Queue<PveTask>();
        private Stopwatch sw;

        public int GetTaskCount()
        {
            return tasks.Count;
        }
        
        public static PveTaskManager Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new PveTaskManager();
                    m_instance.Init();
                }
                return m_instance;
            }
        }

        public void Init()
        {
            sw = new Stopwatch();
            tasks.Clear();
        }

        public void Dispose()
        {
            tasks.Clear();
        }

        public void Update()
        {
            sw.Reset();
            sw.Start();
            while (tasks.Count > 0 && sw.ElapsedMilliseconds < 4)
            {
                var task = tasks.Dequeue();
                task.action.Invoke();
            }
            sw.Stop();
        }

        public void AddTask(Action action,int weight = 10)
        {
            tasks.Enqueue(new PveTask
            {
                action = action,
                weight = 10,
                registTime = Time.time,
            });
        }
    }
}